package cn.tedu._05mvcboot02.controller;

import cn.tedu._05mvcboot02.mapper.UserMapper;
import cn.tedu._05mvcboot02.pojo.dto.UserInsertDTO;

import cn.tedu._05mvcboot02.pojo.entity.User;
import cn.tedu._05mvcboot02.pojo.vo.UserListVO;
import cn.tedu._05mvcboot02.pojo.vo.UserinfoVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import sun.util.calendar.BaseCalendar;

import java.util.Date;
import java.util.List;

//@Controller
@RestController
@RequestMapping("v1/users/")
public class UserController {
    @Autowired
    private UserMapper userMapper;
    @PostMapping("insert")
//    @ResponseBody
    //添加用户
    public String insert(UserInsertDTO userInsertDTO){
        User user = new User();
        BeanUtils.copyProperties(userInsertDTO,user);
        user.setCreated(new Date());
        userMapper.insertUser(user);
        return "用户注册成功";
    }
    @GetMapping("userList")
//    @ResponseBody
    //查询用户列表
    public List<UserListVO> userList(){
        return userMapper.selectUserList();
    }
//删除
    @GetMapping("delete")
//    @ResponseBody
    public String delete(int id){
        userMapper.deleteUserById(id);
        return "删除成功";
    }
    //4修改用户功能
    @PostMapping("update")
//    @ResponseBody
    public String update(User user){
        userMapper.updateUserById(user);
        return "修改成功";
    }
    /*
    5.查询指定用户的用户名和昵称
     */
    @GetMapping("{id}")
    public UserinfoVO getUserInfoById(@PathVariable int id){
        return userMapper.selectUserInfoById(id);
    }


}
